<?php defined('SYSPATH') OR die('No direct access allowed.');

class Report_Controller extends Master_Admin_Controller 
{

		public function index()
		{
			$this->page(0, 0);
		}
		
		public function page()
		{
			$view = new View('admin/report');

			/*
			$view->downloads = '{"data" :  [10, 20, 20, 30, 40, 50, 5, 7] , "song" : { "labels" : [ { value: 1, text: "L1"},'
                         .'{ value: 2, text: "L2"},'
                         .'{ value: 3, text: "Label 3"},'
                         .'{ value: 4, text: "L4"},'
                         .'{ value: 5, text: "L5"},'
                         .'{ value: 6, text: "L6"},'
                         .'{ value: 7, text: "L7"},'
                         .'{ value: 8, text: "Label 8"}'
                         .']}}'; 
			*/
			$view->downloads = '{"data" : [1], "song" : {"labels" : [{value:1, text : "1"}]}}';
			$this->template->content = $view;
			$this->set_title('Report');
			$this->template->render(TRUE);

			kohana::log('debug','Report page');
		}

		public function ajax_page($reportType,$reportCondition,$condition)
		{
			kohana::log('debug','Report : ajax_page');
			if(request::is_ajax())
			{
				$this->auto_render=false;
				$ajax = new ISS_Ajax();
				
				$report = new Report_Model;
			   
				$reportData = $report->getReportData($reportType,$reportCondition,$condition);
			
				$content = '	<div id="reportData" style="display:none;">';
				$content .= '';

				$dataStr = '';
				$songStr = '';
				$count    = 0;

				kohana::log('debug','Report : begin foreach');
				foreach ($reportData as $data): 
					$count = $count + 1;
					
					kohana::log('debug','Report : data amount '.$data['track_no'] );

					if(strlen($dataStr) > 0){
						$dataStr .= ','.$data['amount'];
					}else{
						$dataStr .= '['.$data['amount'];
					}

					if(strlen($songStr) > 0){
						$songStr .= ',{value:'.$count.', text : "'.$data['title'].'"}';
					}else{
						$songStr .= '[{value:'.$count.', text : "'.$data['title'].'"}';
					}

				endforeach;
				kohana::log('debug','Report : end foreach');
				if(strlen($dataStr) > 0){
					$dataStr .= ']';
				}

				if(strlen($songStr) > 0){
					$songStr .= ']';
				}

				if((strlen($songStr) > 0) && (strlen($dataStr) > 0)){
					$content .= '<span id="resultData">{"data" : '.$dataStr.', "song" : {"labels" : '.$songStr.'}}</span>'
										.'<script type="text/javascript">PreviewReport();</script>';
				}else{
					$content .= '<span id="resultData"></span>';
				}

				/*
				$content .= '{"data" :  [10, 20, 20, 30, 40, 50, 5, 7] , "song" : { "labels" : [ { value: 1, text: "L1"},'
								 .'{ value: 2, text: "L2"},'
								 .'{ value: 3, text: "Label 3"},'
								 .'{ value: 4, text: "L4"},'
								 .'{ value: 5, text: "L5"},'
								 .'{ value: 6, text: "L6"},'
								 .'{ value: 7, text: "L7"},'
								 .'{ value: 8, text: "Label 8"}'
								 .']}}';
				*/
				$content .='</div>';

				$selector = "reportData";
				$ajax->addViewUpdate($selector, $content);
				kohana::log('debug','Report result :'.$content);
				echo json_encode($ajax->getHtmlAjax());
			}
			else {
				$this->page(1);
			}
		}
}
